//
//  DYZSelectCarSourceTypeController.m
//  Demo
//
//  Created by 毕仲阳 on 16/8/18.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "DYZSelectCarSourceTypeController.h"
#import "Masonry/Masonry.h"
#import "DSHTTPClient.h"
#import "UIColor+Addition.h"
static NSString *cellid = @"cellid";
@interface DYZSelectCarSourceTypeController ()
@property (nonatomic , strong)NSMutableArray *arrayDatas;
@property(strong,nonatomic)NSArray *datas;
@end

@implementation DYZSelectCarSourceTypeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self sourcetype];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellid];
    self.navigationItem.title = @"选择车源类型";

    self.arrayDatas = [NSMutableArray array];


    //    NSArray *datas = @[
    //                       @{@"title":@"国产"},
    //                       @{@"title":@"进口中规"},
    //                       @{@"title":@"美规"},
    //                       @{@"title":@"中东"},
    //                       @{@"title":@"欧规"},
    //                       @{@"title":@"加版"},
    //                       @{@"title":@"其它"}];
    
    //    self.datas = datas;
    //[self.tableView setSeparatorColor:[UIColor blackColor]];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//获取车型
- (void)sourcetype{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/sourcetype.json.php" withParam:param withSuccessBlock:^(NSDictionary * data) {
        
        NSDictionary *dict = [data objectForKey:@"data"];
        NSMutableArray *array = [NSMutableArray array];
        
        [array addObject:@"不限"];
        
        NSArray *arr = [dict objectForKey:@"attrList"];
        
        for (NSDictionary *dict1 in arr) {
            
            NSString *str = [dict1 objectForKey:@"attrName"];
            
            [array addObject:str];
            
        }
        //        NSLog(@"%@",array);
        self.datas = array.copy;
        [self.tableView reloadData];
        
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
        
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//    return 2;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //    if (section == 0) {
    //
    //        return 1;
    //    }else
    //    {
    //        return self.datas.count;
    //    }
    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
    
    //        UIView *view = [[UIView alloc]init];
    //        view.backgroundColor = [UIColor blackColor];
    //        [cell.contentView addSubview:view];
    //        CGFloat width = self.tableView.bounds.size.width - 30;
    //        [view mas_makeConstraints:^(MASConstraintMaker *make) {
    //            make.height.offset(1.5);
    //            make.width.offset(width);
    //            make.right.offset(0);
    //            make.bottom.equalTo(cell.contentView.mas_bottom);
    //        }];
    
    cell.textLabel.text = self.datas[indexPath.row];
    

    //为全局数组赋值
    [self.arrayDatas addObject:cell.textLabel.text];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view;
    if (section == 0) {
        //view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, [UIScreen mainScreen].scale)];
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 23, 1)];
        view.backgroundColor = [UIColor blackColor];
    }else
    {
        
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.11f;
    }else
    {
        return 20.0f;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(loadSelectCarsSeriesData:)]) {
        [self.delegate loadSelectCarsSeriesData:self.arrayDatas[indexPath.row]];
        NSLog(@"传的值为:%@",self.arrayDatas[indexPath.row]);
    }

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    
}
@end
